<?php 
/**
 * TomatoCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@tomatocms.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2009-2010 TIG Corporation (http://www.tig.vn)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @version 	$Id: show.phtml 446 2010-05-11 10:35:35Z dinhchieu $
 */
?>
<div class="t_g_box t_comment_comment" style="border-top:1px solid #bbbbbb; padding-top:20px; margin-top:10px;">
    <div class="t_g_box_content">
    	<div id="tCommentList">
			<?php if (count($this->comments) > 0) : ?>
			<ul>
				<?php foreach ($this->comments as $index => $comment) : ?>
				<li class="t_comment_comment_level_<?php echo $comment->depth; ?>" style="padding-left: <?php echo $comment->depth * 20; ?>px;">
					<a name="comment-<?php echo $comment->comment_id; ?>"></a>
					<blockquote<?php if ($comment->depth> 0) : ?> class="reply"<?php endif; ?>>
						<?php if ($this->showAvatar && $avatar = $this->helper()->gravatar($comment->email, $this->avatarSize)) : ?>
							<?php if ((null != $comment->web_site && false !== strstr($comment->web_site, 'http://'))) : ?>
								<a target="_blank" href="<?php echo $this->escape($comment->web_site); ?>" ><img src="<?php echo $avatar; ?>" style="float: left; margin: 5px 5px 0 5px;" title="<?php echo $this->escape($comment->full_name); ?>" alt="<?php echo $this->escape($comment->full_name); ?>" /></a>
							<?php else : ?>
								<img src="<?php echo $avatar; ?>" style="float: left; margin: 15px;" title="<?php echo $this->escape($comment->full_name); ?>" alt="<?php echo $this->escape($comment->full_name); ?>" />
							<?php endif; ?>
						<?php endif; ?>
						<div style="float: left; width: 460px">
							<a href="#comment-<?php echo $comment->comment_id; ?>" title="<?php echo $this->escape($comment->title); ?>" class="title"><?php echo $this->escape($comment->title); ?></a><br />
							<?php echo Tomato_Hook_Registry::getInstance()->executeFilter('Comment_Widget_Comment_FormatContent', $comment->content); ?>
						</div>
						<p style="clear: both;"></p>
					</blockquote>
					<cite<?php if ($comment->depth> 0) : ?> class="reply"<?php endif; ?>>
						<strong>
						<?php if ((null != $comment->web_site && false !== strstr($comment->web_site, 'http://'))) : ?>
							<a target="_blank" href="<?php echo $this->escape($comment->web_site); ?>" ><?php echo $this->escape($comment->full_name); ?></a>
						<?php else : ?>
							<?php echo $this->escape($comment->full_name); ?>
						<?php endif; ?>
						</strong> 
						<?php echo sprintf($this->translator()->widget('commented_at'), date($this->globalConfig('datetime')->date_time_format, strtotime($comment->created_date))); ?>
						<?php if ($this->allowComment) : ?><span><a class="tReplyAction" href="javascript: void(0);" rel="<?php echo $comment->comment_id; ?>" title="<?php echo $this->translator()->widget('reply'); ?>"><?php echo $this->translator()->widget('reply'); ?></a></span><?php endif; ?>
					</cite>
				</li>
				<?php endforeach; ?>
			</ul>
			<?php endif; ?>
		</div>
		
		<?php if ($this->allowComment) : ?>
		<div id="t_g_comment">
			<div class="t_g_bottom"><b style="font-size: 16px;font-weight: normal;"><?php echo $this->translator()->widget('write_your_comment'); ?></b></div>
			<?php if (!$this->ok) : ?>
		    <div class="t_g_bottom too_short"><?php echo $this->translator()->widget('send_comment_unsuccess'); ?></div>
		    <?php endif; ?>
		    <div class="t_g_bottom"><?php echo $this->flashMessenger(); ?></div>
			<div class="t_g_bottom">
				<a name="commentForm"></a>
				<form action="" method="post" id="tSendCommentForm" class="t_g_form">
					<input type="hidden" name="tCommentReply" id="tCommentReply" />
					<table width="100%" border="0" cellspacing="0" cellpadding="10">
						<tr>
							<td colspan="2" width="100%"><input type="text" name="tCommentFullName" id="tCommentFullName" value="" style="width: 40%; padding: 3px 0" /></td>
						</tr>
						<tr>
							<td colspan="2" width="100%"><input type="text" name="tCommentEmail" id="tCommentEmail" value="" style="width: 40%; padding: 3px 0" /></td>
						</tr>
						<tr>
							<td style="width: 41%; "><input type="text" name="tCommentWebsite" id="tCommentWebsite" value="" style="width: 100%; padding: 3px 0" /></td>
							<td>
								<span id="tCommentCaptchaQuestion"></span> <input type="text" name="tCommentCaptchaAnswer" id="tCommentCaptchaAnswer" value="" style="width: 40px; padding: 3px 0" />
								<label for="tCommentCaptchaAnswer" generated="true" class="error" style="display: none;"></label>
								<input type="hidden" name="tCommentCaptchaResult" id="tCommentCaptchaResult" value="" />
							</td>
						</tr>
						<tr>
							<td colspan="2" width="100%"><input type="text" name="tCommentTitle" id="tCommentTitle" value="" style="width: 100%; padding: 3px 0" /></td>
						</tr>
						<tr>
							<td colspan="2" width="100%"><textarea style="width: 100%; height: 150px" name="tCommentContent" id="tCommentContent"></textarea></td>
						</tr>
						<tr>
							<td colspan="2" width="100%">
								<input type="hidden" name="tCommentUrl" id="tCommentUrl" value="<?php echo $this->pageUrl; ?>" />
								<input type="button" id="tCommentSendButton" class="t_g_button_comment" value="<?php echo $this->translator()->widget('send_button'); ?>" />
							</td>
						</tr>
					</table>
				</form>
			</div>
		</div>
		<?php endif; ?>
	</div>			    
</div>
<?php if (!$this->isPreviewing) : ?>
<script type="text/javascript" src="<?php echo $this->APP_STATIC_SERVER; ?>/js/tinymce/tiny_mce.js"></script>
<script type="text/javascript" src="<?php echo $this->APP_STATIC_SERVER; ?>/js/jquery.validate/jquery.validate.min.js"></script>
<?php endif; ?>
<script type="text/javascript">
<?php if ($this->allowComment) : ?>
/**
 * Init TinyMCE instances
 */
tinyMCE.init({
	theme: "advanced",
	theme_advanced_buttons1 : "mybutton,bold,italic,underline,separator,strikethrough,justifyleft,justifycenter,justifyright, justifyfull,bullist,numlist,undo,redo,link,unlink",
	theme_advanced_buttons2 : "",
	theme_advanced_buttons3 : "",
	theme_advanced_toolbar_location : "top",
	theme_advanced_toolbar_align : "left",
	width: "100%",
	height: "180",
	mode: "specific_textareas",
	elements: 'tCommentContent',
	// update validation status on change
	onchange_callback: function(editor) {
		tinyMCE.triggerSave();
		$("#" + editor.id).valid();
	}
});

$(document).ready(function() {
	var num1 = Math.floor(Math.random() * 10);
	var num2 = Math.floor(Math.random() * 10);
	var sum = num1 + num2;
	$('#tCommentCaptchaQuestion').html(num1 + ' + ' + num2 + ' = ');
	$('#tCommentCaptchaResult').attr('value' , sum);
	$('#tSendCommentForm').validate({
			rules: {
				tCommentFullName: {
					required: true,
					maxlength: 255
				},
				tCommentEmail: {
					required: true,
					email: true
				},
				tCommentTitle: {
					required: true,
					maxlength: 255
				},
				tCommentContent: {
					required: true,
					minlength: 30,
					maxlength: 800
				},
				tCommentCaptchaAnswer: {
					required: true,
					equalTo: '#tCommentCaptchaResult'
				}
			},
			messages: {
				tCommentFullName: {
					required: '<?php echo $this->translator()->widget('full_name_required'); ?>',
					maxlength: '<?php echo $this->translator()->widget('full_name_maxlength'); ?>'
				},
				tCommentEmail: {
					required: '<?php echo $this->translator()->widget('email_required'); ?>',
					email: '<?php echo $this->translator()->widget('email_invalid'); ?>'
				},
				tCommentTitle: {
					required: '<?php echo $this->translator()->widget('title_required'); ?>',
					maxlength: '<?php echo $this->translator()->widget('title_maxlength'); ?>'
				},
				tCommentContent: {
					required: '<?php echo $this->translator()->widget('content_required'); ?>',
					minlength: '<?php echo $this->translator()->widget('content_minlength'); ?>',
					maxlength: '<?php echo $this->translator()->widget('content_maxlength'); ?>'
				},
				tCommentCaptchaAnswer: {
					required: '<?php echo $this->translator()->widget('captcha_required'); ?>',
					equalTo: '<?php echo $this->translator()->widget('captcha_equalto'); ?>'
				}
			}
		});

	$('.tReplyAction').click(function() {
		var editor = 'tCommentContent';
		toggleEditor(editor);
		var self = this;
		var commentId = $(self).attr('rel');
		$('#tCommentReply').val(commentId);
		var container = $(self).parent().parent().parent();
		$(container).append($('#t_g_comment').css('padding', '10px 0 0'));
		toggleEditor(editor);
		if ($('#tCommentCancelButton').attr('value') == undefined) {
			var cancelButton = document.createElement('input');
			$(cancelButton).attr('id', 'tCommentCancelButton')
					.attr('type', 'button')
					.attr('value', '<?php echo $this->translator()->widget('cancel_reply_button'); ?>')
					.addClass('t_a_button')
					.css('margin-left', '10px')
					.bind('click', function() {
						toggleEditor(editor);
						$('#tCommentReply').val(0);
						$('#tCommentCancelButton').remove();
						$('#tCommentList').after($('#t_g_comment'));
						toggleEditor(editor);
					});
			
			$('#tCommentSendButton').after($(cancelButton));
		}
	});

	function toggleEditor(id) {
		if (!tinyMCE.get(id)) {
			tinyMCE.execCommand('mceAddControl', false, id);
		} else { 
			tinyMCE.execCommand('mceRemoveControl', false, id);
		}
	};
	
	$('#tCommentSendButton').click(function() {
		$('#tSendCommentForm').submit();
	});

	var arr = [
		{
			msg: '<?php echo $this->translator()->widget('full_name'); ?>',
			input: 'tCommentFullName'
		},
		{
			msg: '<?php echo $this->translator()->widget('email'); ?>',
			input: 'tCommentEmail'
		},
		{
			msg: '<?php echo $this->translator()->widget('title'); ?>',
			input: 'tCommentTitle'
		},
		{
			msg: '<?php echo $this->translator()->widget('website'); ?>',
			input: 'tCommentWebsite'
		}
	];
	$(arr).each(function() {
		var input = this.input;
		var msg = this.msg;
		$('#' + input).val(msg).css('color', '#9196A2')
					.focus(function() {
						if ($(this).val() == msg) {
							$(this).val('').css('color', '#000');
						}
					}).blur(function() {
						if ($(this).val() == '') {
							$(this).val(msg).css('color', '#9196A2');
						}
					});
	});
});
<?php endif; ?>

'Tomato.Modules.Comment.Widgets.Comment'.namespace();
Tomato.Modules.Comment.Widgets.Comment.loadComments = function(page) {
	var data = '{"pageIndex": "' + page + '", "page_url": "<?php echo $this->escape($this->pageUrl); ?>", "limit": "<?php echo $this->escape($this->limit); ?>", "show_avatar": "<?php if ($this->showAvatar) : ?>1<?php endif; ?>", "avatar_size": "<?php echo $this->avatarSize; ?>"}';
	var response = Tomato.Core.Widget.Loader.queueAction('comment', 'comment', 'load', data, 'tCommentList');
};
</script>
